/**
 *
 */
package com.jfinal.ext.activiti.model;

import com.hz.foodstalls.controller.BaseController;
import com.hz.foodstalls.interceptor.AdminInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.ext.activiti.WorkFlowService;
import com.jfinal.ext.plugin.activiti.ActivitiPlugin;
import com.jfinal.ext.validator.ActivitiValidator;
import com.jfinal.plugin.activerecord.Page;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Lion
 * @date 2017年2月16日 下午4:04:25
 * @qq 439635374
 */
@Before(AdminInterceptor.class)
public class ModelController extends BaseController {
    String MainPage = "/WEB-INF/views/workflow/index.html";

    public void index() {
        Page<ActReModel> page = ActReModel.dao.getModelPage(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10));
        setAttr("page", page);
        render(MainPage);
    }

    /***
     * 获得列表页
     */
    public void getListPage() {
        render("/WEB-INF/views/workflow/model/list.html");
    }

    /***
     * 获得新增页面
     */
    public void getAddPage() {
        render("/WEB-INF/views/workflow/add.html");
    }

    /***
     * 保存模型--新建新模型
     */
    @Before(ActivitiValidator.class)
    public void save() {
        String name = getPara("name");
        String key = getPara("key");
        try {
            WorkFlowService service = new WorkFlowService();
            service.createModel(ActivitiPlugin.buildProcessEngine(), name, key);
            index();
        } catch (UnsupportedEncodingException e) {
            renderError();
            e.printStackTrace();
        }
    }

    /***
     * 查询分页数据
     */
    public void listData() {
        String curr = getPara("pageNumber");
        String pageSize = getPara("pageSize");
        Page<ActReModel> page = ActReModel.dao.getModelPage(Integer.valueOf(curr), Integer.valueOf(pageSize));
        renderPage(page.getList(), "", page.getTotalRow());
    }

    /***
     * 模型部署
     */
    public void deploy() {
        String id = getPara("id");
        WorkFlowService service = new WorkFlowService();
        String message = service.deploy(id);
        renderSuccess(message);
    }

    /***
     * 删除模型
     */
    public void delete() {
        StringBuilder success = new StringBuilder();
        StringBuilder error = new StringBuilder();
        String[] workFlowIds = getParaValues("ids");
        WorkFlowService service = new WorkFlowService();
        try {
            for (String workFlowId : workFlowIds) {
                service.deleteModel(workFlowId);
            }
            ajaxJsonSuccessMessage("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJsonErrorMessage("删除失败："+e.getMessage());
        }
    }


    /**************************************************************************/
    private String pageTitle = "模型管理";//模块页面标题
    private String breadHomeMethod = "getListPage";//面包屑首页方法

    public Map<String, String> getPageTitleBread() {
        Map<String, String> pageTitleBread = new HashMap<String, String>();
        pageTitleBread.put("pageTitle", pageTitle);
        pageTitleBread.put("breadHomeMethod", breadHomeMethod);
        return pageTitleBread;
    }
}
